package com.jsp.controller.usercontroller;

import com.alibaba.fastjson.JSON;
import com.jsp.entity.UserVO;
import com.jsp.entity.User;
import com.jsp.mapper.impl.DepartMapperImpl;
import com.jsp.mapper.impl.UserMapperImpl;
import com.jsp.util.MD5Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;


@WebServlet("/userSave")
public class UserSaveServlet extends HttpServlet {
    @Override
    //做更新操作
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        response.setStatus(200);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        HashMap<String, Object> data = new HashMap<>();
        try {
            String yhid = new String(request.getParameter("userId").getBytes("iso-8859-1"), "utf-8");
            String yhxm = new String(request.getParameter("username").getBytes("iso-8859-1"), "utf-8");
            String yhkl = request.getParameter("password");
            String yhxb = request.getParameter("sex");
            String yhbm = request.getParameter("depart");
            String csrq = String.join("", request.getParameter("date").split("-"));
            Integer pxh = Integer.valueOf(request.getParameter("sortNum"));
            String id = request.getParameter("id");
            UserMapperImpl userMapper = new UserMapperImpl();
            User userMapperById = userMapper.findById(id);
            if (Objects.isNull(userMapperById)) {
                User userMapperByUser = userMapper.findByUsername(yhid);
                if (!Objects.isNull(userMapperByUser)) {
                    response.getWriter().print(-1);
                    return;
                }
                User user = new User();
                user.setYhid(yhid);
                user.setYhxm(yhxm);
                user.setYhkl(MD5Utils.getEncode(yhkl, yhid));
                user.setYhxb(yhxb);
                user.setYhbm(yhbm);
                user.setCsrq(csrq);
                user.setPxh(pxh);
                // 密码加密
                user.setYhkl(MD5Utils.getEncode(user.getYhkl(), user.getYhid()));
                // 新增
                userMapper.addUser(user);
            } else {
                userMapperById.setYhid(yhid);
                userMapperById.setYhxm(yhxm);
                userMapperById.setYhkl(MD5Utils.getEncode(yhkl, yhid));
                userMapperById.setYhxb(yhxb);
                userMapperById.setYhbm(yhbm);
                userMapperById.setCsrq(csrq);
                userMapperById.setPxh(pxh);
                userMapper.updateUser(userMapperById);
            }
            response.getWriter().print(1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
